package com.tianyu.sports.handle;

import com.tianyu.sports.bean.Result;
import com.tianyu.sports.exception.VipException;
import com.tianyu.sports.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

public class ExceptionHandle {
    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handler(Exception e){
        if( e instanceof VipException){
            VipException userException = (VipException) e;
            return ResultUtil.error( userException.getCode(), userException.getMessage());
        }else {
            logger.info("[系统异常] {}",e);
            return ResultUtil.error( -1, "未知错误");
        }
    }
}
